package com.hudongwx.my_shian.adapter.communityAdapter.recommendAdapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hudongwx.my_shian.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.xutils.x;

/**
 * Author:曾传标
 * time：16-12-15 下午5:15
 */
public class MySunAdapter extends BaseAdapter {

    JSONArray data;
    Context context;

    public MySunAdapter(JSONArray data, Context context) {
        this.data = data;
        this.context = context;
    }


    @Override
    public int getCount() {
        return data.length();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        JSONArray arr = null;
        try {
           arr= data.getJSONObject(position).getJSONArray("ListFile");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (null==convertView){//没有可以复用的布局
            //解析布局
            convertView = LayoutInflater.from(context).inflate(R.layout.recommend_sun_item,null);
            //实例化一个ViewHolder
            viewHolder = new ViewHolder();

            //找出需要显示的控件
            viewHolder.textView1 = (TextView) convertView.findViewById(R.id.recommend_sun_item_tv1);
            viewHolder.textView2 = (TextView) convertView.findViewById(R.id.recommend_sun_item_tv2);

            viewHolder.imageView1 = (ImageView) convertView.findViewById(R.id.recommend_sun_item_iv1);
            viewHolder.imageView2 = (ImageView) convertView.findViewById(R.id.recommend_sun_item_iv2);
            viewHolder.imageView3 = (ImageView) convertView.findViewById(R.id.recommend_sun_item_iv3);

            convertView.setTag(viewHolder);
        }else {
            //有可以复用的布局
            viewHolder = (ViewHolder) convertView.getTag();
        }

        //把数据设置给控件
        try {
            if (arr.length()==1){
                x.image().bind(viewHolder.imageView1,data.getJSONObject(position).getJSONArray("ListFile").getJSONObject(0).getString("Url"));
            }else if (arr.length()==2){
                x.image().bind(viewHolder.imageView1,data.getJSONObject(position).getJSONArray("ListFile").getJSONObject(0).getString("Url"));
                x.image().bind(viewHolder.imageView2,data.getJSONObject(position).getJSONArray("ListFile").getJSONObject(1).getString("Url"));

            }else if (arr.length()>=3){
                x.image().bind(viewHolder.imageView1,data.getJSONObject(position).getJSONArray("ListFile").getJSONObject(0).getString("Url"));
                x.image().bind(viewHolder.imageView2,data.getJSONObject(position).getJSONArray("ListFile").getJSONObject(1).getString("Url"));
                x.image().bind(viewHolder.imageView3,data.getJSONObject(position).getJSONArray("ListFile").getJSONObject(2).getString("Url"));
            }


            viewHolder.textView1.setText(data.getJSONObject(position).getString("Title"));
            viewHolder.textView2.setText(data.getJSONObject(position).getString("Content"));
//
//            Log.i("666666666666666666", "getView: "+(position));
//            Log.i("666666666666666666", "getView: "+(data.getJSONObject(position).getString("Title")));
//            Log.i("666666666666666666", "getView: "+(data.getJSONObject(position).getString("Content")));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return convertView;
    }


    class ViewHolder{
        ImageView imageView1;
        ImageView imageView2;
        ImageView imageView3;

        TextView textView1;
        TextView textView2;
    }

}
